package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.*;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService{
    @Autowired
    private RoleDao roleDao;

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private PermissionDao permissionDao;


    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public void delete(Integer id) {
        roleDao.deleteByPermissionId(id);
        roleDao.deleteByMenuId(id);
        roleDao.deleteByRoleId(id);
    }

    @Override
    public void edit(Role role) {

    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer id) {
        return roleDao.findRoleIdsByUserId(id);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Map findMaxAll() {
        List<Menu> menuDaoAll = menuDao.findAll();
        List<Permission> all = permissionDao.findAll();
        Map<String, List> map = new HashMap<>();
        map.put("menuDaoAll", menuDaoAll);
        map.put("all", all);
        return map;
    }

    @Override
    public void add(Role role, Integer[] PermissionIds, Integer[] MenuIds) {
        roleDao.add(role);
        setRoleAndPermission(role.getId(),PermissionIds);
        setRoleAndMenu(role.getId(),MenuIds);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }

    @Override
    public List<Role> findByIds(Integer id) {
        return null;
    }



    @Override
    public List<Integer> findPermissionByRole(Integer id) {
        return roleDao.findPermissionByRole(id);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        delete(role.getId());
        roleDao.add(role);
        setRoleAndPermission(role.getId(),permissionIds);
        setRoleAndMenu(role.getId(),menuIds);
    }

    private void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                roleDao.setRoleAndPermission(roleId,permissionId);
            }



        }
    }
    private void setRoleAndMenu(Integer roleId,Integer[] MenuIds) {
        for (Integer menuId : MenuIds) {
            roleDao.setRoleAndMenu(roleId,menuId);
        }
    }
}
